package com.buddy.learn.algorithm.letcode;

/**
 * @author Buddy
 * @date 2021/5/31
 */
public class _014字符串最长公共前缀 {

    public static void main(String[] args) {
        String[] strs = new String[]{"flower", "flow", "flight"};
        String str = longestCommonPrefix(strs);
        System.out.println(str);
    }

    private static String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }

        int count = strs.length;
        int length = strs[0].length();
        for (int i = 0; i < length; i++) {
            char c = strs[0].charAt(i);
            for (int j = 1; j < count; j++) {
                if (strs[j].length() == i || strs[j].charAt(i) != c) {
                    return strs[0].substring(0, i);
                }
            }
        }
        return strs[0];
    }
}
